package de.homelabs.bookmarkmanager.desktop.qt.view;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import com.trolltech.qt.core.QModelIndex;
import com.trolltech.qt.gui.QHBoxLayout;
import com.trolltech.qt.gui.QMainWindow;
import com.trolltech.qt.gui.QWidget;

import de.homelabs.bookmarkmanager.desktop.qt.domain.HBookmarkTreeModel;
import de.homelabs.bookmarkmanager.domain.BookmarkType;
import de.homelabs.bookmarkmanager.persistence.domain.HBookmark;
import de.homelabs.bookmarkmanager.service.IHContextMenuHandler;

public class HMainWidget extends QWidget {

	private HTreeView treeView;
	
	private IHContextMenuHandler contextMenuHandler;
	
	//bookmarks
	private List<HBookmark> bookmarks = new ArrayList<HBookmark>();
	private HBookmarkTreeModel bmt; 
	
	private HBookmarkDetailView detailView = new HBookmarkDetailView(this);
	
	/**
	 * constructor
	 * @param contextMenuHandler
	 * @param parent
	 * @throws MalformedURLException
	 */
	public HMainWidget(IHContextMenuHandler contextMenuHandler, BookmarkManagerMainView parent) throws MalformedURLException{
		//set parent, is the main window
		super(parent);
		
		this.contextMenuHandler = contextMenuHandler;
		//
		QHBoxLayout hbox = new QHBoxLayout();
		
		/********************
		 * 		left		*
		 ********************/	
		treeView = new HTreeView(this);
		treeView.setContextMenuHandler(this.contextMenuHandler);
		
		//layout
		treeView.header().hide();
		
		//add bookmark model
		
		HBookmark bm = new HBookmark("Eintrag eins", new URL("http://test.de"), "test" ,null, BookmarkType.BOOKMARK);
		bookmarks.add(bm);
		
		bmt = new HBookmarkTreeModel(bookmarks);
		treeView.setModel(bmt);
		
		//register treeview clicked signal
		treeView.clicked.connect(this, "onTreeViewClicked(QModelIndex)");
		
		/********************
		 * 		right		*
		 ********************/
		//fit together
		//hbox.addWidget(treeWidget);
		hbox.addWidget(treeView);
		hbox.addWidget(detailView);
		
		this.setLayout(hbox);
		
		//System.out.println("parent: "+this.parent());
	}
	
	public void messageToStatusbar(String message){
		BookmarkManagerMainView parent = (BookmarkManagerMainView)this.parent();
		parent.messageToStatusBar(message, true);
	}
	
	/****************************************************
	 *  				event handling					*				
	 ****************************************************/
	
	/**
	 * treeView clicked signal handler
	 * @param QModelIndex mIndex - reference to model
	 */
	protected void onTreeViewClicked(QModelIndex mIndex){
		System.out.println("[HMainWidget] "+mIndex);
		
		//Extract Object from view/model
		HBookmarkTreeModel bmt = (HBookmarkTreeModel) mIndex.model();
		HBookmark bookmark = (HBookmark) bmt.indexToValue(mIndex);
		
		detailView.setViewData(bookmark);
	}
	
	
	/*******************
	 * getter / setter *
	 *******************/
//	public HTreeWidget getTreeWidget() {
//		return treeWidget;
//	}
//
//	public void setTreeView(HTreeWidget treeWidget) {
//		this.treeWidget = treeWidget;
//	}
}
